﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BO;
using BO.Admin;

namespace ProjectQuaDep.Admin.Products
{
    public partial class ProductList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showListMenuLv1();
                setSubMenu();
                showListMenuLv1Find();
                showAll();
                ShowCompany();
                BinCBLNhanVien();
            }
        }
        protected void showAll()
        {
            string sFind = txtSearch.Text.Trim();
            string sZoneID = "0";
            string checkStatus = rblStatus.SelectedValue.ToString();
            sZoneID = ddlZoneFind.SelectedValue.ToString();
            string typeFind = rbTypeFind.SelectedValue.ToString();
            gvShow.DataSource = ProductsBO.getAllProductsBO(sFind, sZoneID, typeFind, checkStatus);
            gvShow.DataBind();
        }
        protected void BinFilter()
        {
            int ZoneID = int.Parse(ddlZone.SelectedValue.ToString());
            cbListFinter.DataSource = BO.ProductUserBO.ProductUBO.GetListFilter(ZoneID);
            cbListFinter.DataValueField = "Value";
            cbListFinter.DataTextField = "Name";
            cbListFinter.DataBind();
        }
        protected void ShowCompany()
        {
            ddlCompany.DataSource = BO.Admin.CompanyBO.GetAllCompanyByStatusBO("1");
            ddlCompany.DataValueField = BO.Const.tbCompanies.CompanyID;
            ddlCompany.DataTextField = BO.Const.tbCompanies.NameCp;
            ddlCompany.DataBind();
        }
        protected void showListMenuLv1()
        {
            ddlZone.DataSource = ProductsBO.getAllZoneCache();
            ddlZone.DataTextField = BO.Const.tbZoneNews.title;
            ddlZone.DataValueField = BO.Const.tbZoneNews.ZoneId;
            ddlZone.DataBind();
        }
        protected void showListMenuLv1Find()
        {
            DataTable dtListAll = ProductsBO.getAllZoneListBO();
            DataTable dtListFilterTo = new DataTable();
            DataTable dtListFilterNho = new DataTable();
            DataTable dtShow = new DataTable();
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.title);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.title);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.ZoneId);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.title);
            dtShow.Columns.Add(BO.Const.tbZoneProduct.ParentId);
            DataRow drFirst = dtShow.NewRow();
            drFirst[BO.Const.tbZoneProduct.ZoneId] = "0";
            drFirst[BO.Const.tbZoneProduct.title] = "Tất cả";
            drFirst[BO.Const.tbZoneProduct.ParentId] = "0";
            dtShow.Rows.Add(drFirst);
            //chia ra de add sau
            if (dtListAll.Rows.Count > 0)
            {
                for (int i = 0; i < dtListAll.Rows.Count; i++)
                {
                    if (int.Parse(dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString()) > 0)
                    {
                        //cat nho
                        DataRow dr = dtListFilterNho.NewRow();
                        dr[BO.Const.tbZoneProduct.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString();
                        dr[BO.Const.tbZoneProduct.title] = dtListAll.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                        dr[BO.Const.tbZoneProduct.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString();
                        dtListFilterNho.Rows.Add(dr);
                    }
                    else
                    {
                        //cat to
                        DataRow dr = dtListFilterTo.NewRow();
                        dr[BO.Const.tbZoneProduct.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString();
                        dr[BO.Const.tbZoneProduct.title] = dtListAll.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                        dr[BO.Const.tbZoneProduct.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString();
                        dtListFilterTo.Rows.Add(dr);
                    }
                }
            }
            //add vao
            if (dtListFilterTo.Rows.Count > 0)
            {
                for (int i = 0; i < dtListFilterTo.Rows.Count; i++)
                {
                    //add cat to sau do add luon cat con vao
                    DataRow dr = dtShow.NewRow();
                    int ParentId = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.ParentId].ToString());
                    int ID = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.ZoneId].ToString());
                    dr[BO.Const.tbZoneProduct.ZoneId] = ID.ToString();
                    dr[BO.Const.tbZoneProduct.title] = dtListFilterTo.Rows[i][BO.Const.tbZoneProduct.title].ToString();
                    dr[BO.Const.tbZoneProduct.ParentId] = ParentId;
                    dtShow.Rows.Add(dr);
                    for (int j = 0; j < dtListFilterNho.Rows.Count; j++)
                    {
                        int ParentId2 = int.Parse(dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ParentId].ToString());
                        if (ParentId2 == ID)
                        {
                            DataRow dr2 = dtShow.NewRow();
                            dr2[BO.Const.tbZoneProduct.ZoneId] = dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ZoneId].ToString();
                            dr2[BO.Const.tbZoneProduct.title] = "---" + dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.title].ToString();
                            dr2[BO.Const.tbZoneProduct.ParentId] = dtListFilterNho.Rows[j][BO.Const.tbZoneProduct.ParentId].ToString();
                            dtShow.Rows.Add(dr2);
                        }
                    }
                }
            }


            ddlZoneFind.DataSource = dtShow;
            ddlZoneFind.DataTextField = BO.Const.tbZoneProduct.title;
            ddlZoneFind.DataValueField = BO.Const.tbZoneProduct.ZoneId;
            ddlZoneFind.DataBind();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string ZoneId = ddlZone.SelectedValue.ToString();
            string ZoneIdOther = "";
            string Title = txtTitle.Text;
            string Sapo = txtDesc.Text;
            string Avatar = GetLinkImages();
            string Author = txtTacGia.Text;
            string IsFocus = Utils.getOneSelect(ckFocus);
            string parentID = "0";
            string Tags = txtTags.Text;
            string status = Utils.getOneSelect(ckSelect);
            string longContent = txtContent.Text;
            string price = txtPrice.Text;
            string sale = txtSale.Text;
            string CompanyID = ddlCompany.SelectedValue.ToString();
            string TypeSale = ddlTypeSale.SelectedValue.ToString();
            string ngayConMoi = txtNgayConMoi.Text.Trim();
            string baoHanh = txtBaoHanh.Text.Trim();
            string dkLoc = "";//txtFilter.Text.Trim();
            for (int i = 0; i < cbListFinter.Items.Count; i++)
            {
                //lay ra danh sach loc
                if (cbListFinter.Items[i].Selected == true)
                {
                    dkLoc += cbListFinter.Items[i].Value.ToString() + ",";
                }
            }
            string ProductCode = txtProductCode.Text.Trim();
            string thongSoKyThuat = "";
            string NewsRelation = "";
            for (int i = 0; i < cblNhanVien.Items.Count; i++)
            {
                //lay ra danh sach thuoc chuyen muc nao
                if (cblNhanVien.Items[i].Selected == true)
                {
                    thongSoKyThuat += cblNhanVien.Items[i].Value.ToString() + ",";
                }
            }
            ngayConMoi = BO.Utils.ShowDatetimeyyyyMMdd(ngayConMoi, "dd/MM/yyyy", "yyyyMMdd");
            string checkQuanTity = ddlCheckQuantity.SelectedValue.ToString();
            string nameZoneSmall = "";
            for (int i = 0; i < cblZoneOther.Items.Count; i++)
            {
                //lay ra danh sach thuoc chuyen muc nao
                if (cblZoneOther.Items[i].Selected == true)
                {
                    ZoneIdOther += cblZoneOther.Items[i].Value.ToString() + ",";
                    parentID = cblZoneOther.Items[i].Value.ToString();
                    nameZoneSmall = cblZoneOther.Items[i].Text.ToString();
                }
            }
            string Url = "/" + Utils.BuildLinkUrlNice(nameZoneSmall) + "/" + Utils.BuildLinkUrlNice(Title.Trim()) + "-";
            //danh sach san pham lien quan
            for (int i = 0; i < rbNewsRelation.Items.Count; i++)
            {
                //lay ra danh sach thuoc chuyen muc nao
                if (rbNewsRelation.Items[i].Selected == true)
                {
                    NewsRelation += rbNewsRelation.Items[i].Value.ToString() + ",";
                }
            }
            txtSanPhamLienQuan.Text = NewsRelation;
            //
            if (cmdUpdate.Text == "Thêm Mới")
            {
                //them moi
                //them company vao bang zone da
                ProductsBO.EditZoneCompanyBO(CompanyID, ddlZone.SelectedValue.ToString() + ",");
                ProductsBO.EditZoneCompanyBO(CompanyID, cblZoneOther.SelectedValue.ToString() + ",");
                string kq = ProductsBO.addProductBO(ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent, price, sale, CompanyID, TypeSale, checkQuanTity, ngayConMoi, baoHanh, dkLoc, thongSoKyThuat, NewsRelation, ProductCode);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi thêm mới bạn hãy xem lại!";
                }
                else
                {
                    ProductsBO.UpdateBuildLinkBO();
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
            else
            {
                //sua chua
                //them company vao bang zone da
                ProductsBO.EditZoneCompanyBO(CompanyID + ",", ddlZone.SelectedValue.ToString());
                ProductsBO.EditZoneCompanyBO(CompanyID + ",", cblZoneOther.SelectedValue.ToString());
                Url = Url + "p" + lblID.Text.Trim() + ".htm";
                string kq = ProductsBO.EditProductBO(lblID.Text, ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent, price, sale, CompanyID, TypeSale, checkQuanTity, ngayConMoi, baoHanh, dkLoc, thongSoKyThuat, NewsRelation, ProductCode);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi cập nhật bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            showAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                DataTable dt = ProductsBO.GetInfomationProductBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtContent.Text = dt.Rows[0][Const.tbProducts.longContent].ToString();
                    txtDesc.Text = dt.Rows[0][Const.tbProducts.Sapo].ToString();

                    txtTacGia.Text = dt.Rows[0][Const.tbProducts.Author].ToString();
                    txtTags.Text = dt.Rows[0][Const.tbProducts.Tags].ToString();
                    txtTitle.Text = dt.Rows[0][Const.tbProducts.Title].ToString();
                    txtPrice.Text = dt.Rows[0][Const.tbProducts.Price].ToString();
                    txtSale.Text = dt.Rows[0][Const.tbProducts.Sale].ToString();
                    txtBaoHanh.Text = dt.Rows[0][Const.tbProducts.Warranty].ToString();
                    txtProductCode.Text = dt.Rows[0][Const.tbProducts.ProductCode].ToString();
                    //san pham lien quan
                    string tinLienQuan = dt.Rows[0][Const.tbProducts.NewsRelation].ToString();
                    BinSanPhamLienQuan(tinLienQuan);
                    txtSanPhamLienQuan.Text = tinLienQuan;
                    Utils.setOneSelect(ckFocus, dt.Rows[0][Const.tbProducts.IsFocus].ToString());
                    Utils.setOneSelect(ckSelect, dt.Rows[0][Const.tbProducts.status].ToString());
                    ddlCompany.SelectedValue = dt.Rows[0][Const.tbProducts.CompanyID].ToString();
                    ddlTypeSale.SelectedValue = dt.Rows[0][Const.tbProducts.TypeSale].ToString();
                    ddlCheckQuantity.SelectedValue = dt.Rows[0][Const.tbProducts.CheckQuantity].ToString();
                    txtNgayConMoi.Text = BO.Utils.ShowDatetimeyyyyMMdd(dt.Rows[0][Const.tbProducts.EndDateNew].ToString(), "yyyyMMdd", "dd/MM/yyyy");
                    //set link images
                    //txtImages.Text = dt.Rows[0][Const.tbProducts.Avatar].ToString();
                    SetLinkImages(dt.Rows[0][Const.tbProducts.Avatar].ToString());
                    ddlZone.SelectedValue = dt.Rows[0][Const.tbProducts.ZoneId].ToString();
                    string OtherCat = dt.Rows[0][Const.tbProducts.ZoneIdOther].ToString();
                    setSubMenu();
                    for (int i = 0; i < cblZoneOther.Items.Count; i++)
                    {
                        //lay ra danh sach thuoc chuyen muc nao
                        //if (cblZoneOther.SelectedItem.Selected == true)
                        //{
                        string sValues = cblZoneOther.Items[i].Value.ToString() + ",";//dinh dang la cat, vd 12,
                        if (OtherCat==sValues)
                        {
                            cblZoneOther.Items[i].Selected = true;
                        }
                        else
                        {
                            cblZoneOther.Items[i].Selected = false;
                        }
                        //}
                    }
                    //hien ra danh sach nhan vien
                    string ListNhanVien = dt.Rows[0][Const.tbProducts.Specifications].ToString().Trim();
                    for (int i = 0; i < cblNhanVien.Items.Count; i++)
                    {
                        string sValues = cblNhanVien.Items[i].Value.ToString() + ",";//dinh dang la cat, vd 12,
                        if (ListNhanVien.Contains(sValues))
                        {
                            cblNhanVien.Items[i].Selected = true;
                        }
                        else
                        {
                            cblNhanVien.Items[i].Selected = false;
                        }
                    }
                    //
                    //hien ra bo loc
                    string sFilter = dt.Rows[0][Const.tbProducts.FilterConditions].ToString();
                    BinFilter();
                    for (int i = 0; i < cbListFinter.Items.Count; i++)
                    {
                        //lay ra danh sach thuoc chuyen muc nao
                        //if (cblZoneOther.SelectedItem.Selected == true)
                        //{
                        string sValues = cbListFinter.Items[i].Value.ToString() + ",";//dinh dang la cat, vd 12,
                        if (sFilter.Contains(sValues))
                        {
                            cbListFinter.Items[i].Selected = true;
                        }
                        else
                        {
                            cbListFinter.Items[i].Selected = false;
                        }
                        //}
                    }
                    //
                }
            }
            else if (cmdName == "cmdNewsRelation")
            {
                //tin lien quan
                if (pnUpdate.Visible == false)
                {
                    lblPage.Text = "Hiện không có thay đổi nên tác vụ không thực hiện được!";
                }
                else
                {
                    if (txtSanPhamLienQuan.Text.Contains(ID + ","))
                    {
                        lblMsTinLienQuan.Text = "Sản phẩm liên quan đã tồn tại!";
                    }
                    else
                    {
                        lblPage.Text = "";
                        lblMsTinLienQuan.Text = "Thêm sản phẩm liên quan thành công!";
                        txtSanPhamLienQuan.Text += ID + ",";
                    }
                    BinSanPhamLienQuan(txtSanPhamLienQuan.Text);
                    txtSearch.Focus();
                }
            }
            else if (cmdName == "cmdRemove")
            {
                ProductsBO.EditStatusProductBO(ID, "0");
                showAll();
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            showAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm Mới";
            RsControl();
            BinFilter();
        }

        protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            setSubMenu();
            BinFilter();
        }
        protected void RsControl()
        {
            lblID.Text = "";
            txtProductCode.Text = "";
            txtContent.Text = "";
            txtDesc.Text = "";
            txtImages.Text = "";
            txtTacGia.Text = "";
            txtTags.Text = "";
            txtTitle.Text = "";
            txtPrice.Text = "0";
            txtSale.Text = "0";
            txtImages2.Text = "";
            txtImages3.Text = "";
            txtImages4.Text = "";
            txtImages5.Text = "";
            txtNgayConMoi.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtBaoHanh.Text = "";
            txtFilter.Text = "";
            ckFocus.Checked = false;
            ckSelect.Checked = false;
            txtSanPhamLienQuan.Text = "";
            rbNewsRelation.Items.Clear();
            //cblNhanVien.Items.Clear();
            //cblZoneOther.Items.Clear();
            showListMenuLv1();
            setSubMenu();
            ShowCompany();
            BinCBLNhanVien();

        }
        protected void setSubMenu()
        {
            DataTable dtZoneSub = new DataTable();
            dtZoneSub = ProductsBO.getAllZoneByParentIdCache(ddlZone.SelectedValue.ToString());
            cblZoneOther.DataSource = dtZoneSub;
            cblZoneOther.DataValueField = BO.Const.tbZoneNews.ZoneId;
            cblZoneOther.DataTextField = BO.Const.tbZoneNews.title;
            cblZoneOther.DataBind();
        }

        protected string GetLinkImages()
        {
            string kq = "";
            string avatar1 = txtImages.Text.Trim();
            string avatar2 = txtImages2.Text.Trim();
            string avatar3 = txtImages3.Text.Trim();
            string avatar4 = txtImages4.Text.Trim();
            string avatar5 = txtImages5.Text.Trim();
            kq = avatar1 + "," + avatar2 + "," + avatar3 + "," + avatar4 + "," + avatar5;
            return kq;
        }
        protected void SetLinkImages(string arrLinkImg)
        {
            if (arrLinkImg.Contains(","))
            {
                //co nhieu anh
                string[] arrImg = arrLinkImg.Split(',');
                try
                {
                    txtImages.Text = arrImg[0];
                    txtImages2.Text = arrImg[1];
                    txtImages3.Text = arrImg[2];
                    txtImages4.Text = arrImg[3];
                    txtImages5.Text = arrImg[4];
                }
                catch (Exception)
                { }
            }
            else
            {
                //co 1 anh thoi
                txtImages.Text = arrLinkImg;
            }
        }
        protected void BinCBLNhanVien()
        {
            DataTable dtNhanVien = EmployeeBO.GetAllEmployeeBO("1");
            if (dtNhanVien.Rows.Count > 0)
            {
                cblNhanVien.DataSource = dtNhanVien;
                cblNhanVien.DataValueField = Const.tbEmployee.ID;
                cblNhanVien.DataTextField = Const.tbEmployee.Name;
                cblNhanVien.DataBind();
            }
        }
        protected void BinSanPhamLienQuan(string NewsRelation)
        {
            DataTable dtTlq = ProductsBO.getAllProductsByNewsRelationBO(NewsRelation.TrimEnd(','));
            if (dtTlq.Rows.Count > 0)
            {
                rbNewsRelation.DataSource = dtTlq;
                rbNewsRelation.DataValueField = Const.tbProducts.id;
                rbNewsRelation.DataTextField = Const.tbProducts.Title;
                rbNewsRelation.DataBind();
                for (int i = 0; i < rbNewsRelation.Items.Count; i++)
                {
                    rbNewsRelation.Items[i].Selected = true;
                }
            }
            else
            {
                rbNewsRelation.Items.Clear();
            }
        }

        protected void ddlZoneFind_SelectedIndexChanged(object sender, EventArgs e)
        {
            showAll();
        }

        protected void rbTypeFind_SelectedIndexChanged(object sender, EventArgs e)
        {
            showAll();
        }

        protected void rblStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            showAll();
        }

    }
}